package com.sky.controller.admin;

import com.aliyun.oss.OSS;
import com.sky.constant.MessageConstant;
import com.sky.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class CommonController {
    @Autowired
    private OSS ossClient;

    @Value("${sky.alioss.bucket-name}")
    private String bucketName;
    @Value("${sky.alioss.endpoint}")
    private String endpoint;

    /**
     * 阿里云oss对象存储图片
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(MultipartFile file) {
        /**
         * 1.添加依赖：
         <dependency>
         <groupId>com.aliyun.oss</groupId>
         <artifactId>aliyun-sdk-oss</artifactId>
         <version>3.10.2</version>
         </dependency>
         2.配置阿里云OSS：
         aliyun.oss.endpoint=your-endpoint
         aliyun.oss.accessKeyId=your-access-key-id
         aliyun.oss.accessKeySecret=your-access-key-secret
         aliyun.oss.bucketName=your-bucket-name
         3.创建OSS客户端：
         @Configuration public class OssConfig {

         @Value("${sky.alioss.endpoint}") private String endpoint;

         @Value("${sky.alioss.access-key-id}") private String accessKeyId;

         @Value("${sky.alioss.access-key-secret}") private String accessKeySecret;

         @Value("${sky.alioss.bucket-name}") private String bucketName;

         @Bean public OSS ossClient() {
         return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
         }

         @Bean public String ossBucketName() {
         return bucketName;
         }
         }
         4.上传图片：
         @RestController
         @RequestMapping("/admin/common") public class CommonController {

         @Autowired private OSS ossClient;

         @Value("${aliyun.oss.bucketName}") private String bucketName;

         @PostMapping("/upload") public String uploadImage(@RequestParam("file") MultipartFile file) {
         try {
         // 获取输入流
         InputStream inputStream = file.getInputStream();

         // 上传文件
         String objectName = "images/" + file.getOriginalFilename();
         ossClient.putObject(bucketName, objectName, inputStream);

         // 返回图片的URL
         String url = "https://" + bucketName + ".oss-" + ossClient.getEndpoint().getHost() + "/" + objectName;
         return url;
         } catch (Exception e) {
         e.printStackTrace();
         return "上传失败";
         }
         }
         }
         */
        //注意:阿里云OSS存储图片,首先需要创建一个bucket,然后设置屏幕右边头像acesskey,再设置bucket桶的权限:将权限控制,阻止公共访问关闭掉,读写权限改为公共读写,这样产能访问到图片
        //access-key-secret是唯一一次查看的机会
        try {
            // 获取输入流
            InputStream inputStream = file.getInputStream();
            //            创建新的文件名
            String originalFilename = file.getOriginalFilename();
            String objectFileName = UUID.randomUUID().toString()+ originalFilename.substring(originalFilename.lastIndexOf("."));
            // 上传文件
            String objectName = "images/" + objectFileName;
            ossClient.putObject(bucketName, objectName, inputStream);
            // 返回图片的URL
            //https://sky-take-out.oss-cn-beijing.aliyuncs.com/images/example.jpg,
            String url = "https://" + bucketName  +"."+ endpoint + "/" + objectName;


            return Result.success(url);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.UPLOAD_FAILED);
        }

    }
}
